package Vis;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import comms.Communicator;
import comms.Device;

/**
 * A generic panel to display sensor data 
 * 
 * @author norrielm
 *
 */
public class DevicePanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private String id = null; //device id
	//private float[] accel = null; //accel value
	private Users usersPanel;
	private JTextArea debugStatus;
	private long commsDelay;
	private long lastTime;
	
	private final int WIDTH = 450; 
	
	//labels
	JLabel //accelLabel,
		usersLabel, delayLabel;
	private JScrollPane scroll;
	private Device device;
	
	public DevicePanel(Device d, String deviceName, boolean hasAccel, boolean hasUsers){
		device = d;
		//this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createTitledBorder(deviceName.toUpperCase()));
		//setSize(new Dimension(500,500));
		setPreferredSize(new Dimension(WIDTH,150));
		//setMinimumSize(new Dimension(500,500));
		id = deviceName;	
		//this.add(new JLabel(String.format("Device id: %s", id)));
		
		JPanel labelsPanel = new JPanel();
		labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.Y_AXIS));
		
		if(hasAccel){
		/* accel = new float[]{0f,0f,0f};
			accelLabel = new JLabel(String.format("Accel: (%f,%f,%f)", accel[0],accel[1],accel[2]));
			labelsPanel.add(accelLabel);*/
			
			addDeviceButtons();
		}
		if(hasUsers){
			usersPanel = new Users();
			usersLabel = new JLabel(String.format("User count: %d", usersPanel.userCount()));
			
			labelsPanel.add(usersPanel);
			labelsPanel.add(usersLabel);
			
			this.addKinectButtons();
		}
		this.add(labelsPanel);
		
		this.addBasicLabels();

		//do/add to frame above
		//JScrollPane scroll2 = new JScrollPane(this,
			//	JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				//JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		this.validate();
	}
	
	public void setName(String s){
		id = s;
		updateLabels();
	}
	
	public Users getUsers(){
		return usersPanel;
	}
	
	/* ***** Setup screen ******* */
	
	public void addBasicLabels(){
		
		/*
		commsDelay = 0;
		lastTime = System.currentTimeMillis();
		delayLabel =new JLabel(String.format("Comms Delay: %.2f s", commsDelay/1000f)); 
		this.add(delayLabel);
		*/
		
		//this.setPreferredSize(new Dimension(WIDTH,500));

		//Debug status
		debugStatus = new JTextArea();
		debugStatus.setEditable(false);
		//debugStatus.setPreferredSize(new Dimension(WIDTH, 250));
		debugStatus.setEditable(false);
		
		scroll = new JScrollPane(debugStatus,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scroll.setBorder(BorderFactory.createTitledBorder("Debug log"));
		scroll.setPreferredSize(new Dimension(WIDTH-100, 150));
		//scroll.setSize(new Dimension(500,500));
		//scroll.setVisible(true);
	//	scroll.add(debugStatus);
		
		this.add(scroll, BorderLayout.EAST);		
	}
	
	public void addKinectButtons(){
		JCheckBox c = new JCheckBox("Log hand data");
		c.setMnemonic(KeyEvent.VK_H);
		c.setSelected(false);
		c.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent i) {
				if(i.getStateChange()==ItemEvent.SELECTED){
					Communicator.c.doLogging(true);
					System.out.println("Logging on");
				}
				else{
					Communicator.c.doLogging(false);
					System.out.println("Logging off");
				}
			}	
		});
		
		this.add(c, BorderLayout.NORTH);
	}
	
	public void addDeviceButtons(){
		JCheckBox c = new JCheckBox("Log data on device");
		c.setMnemonic(KeyEvent.VK_L);
		c.setSelected(false);
		c.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent i) {
				if(i.getStateChange()==ItemEvent.SELECTED){
					Communicator.s.doLog(true, device.getIPAddress().toString());
					System.out.println("Logging on");
				}
				else{
					Communicator.s.doLog(false,device.getIPAddress().toString());
					System.out.println("Logging off");
				}
			}	
		});
		
		/*JButton b = new JButton("Vibrate");
		c.setMnemonic(KeyEvent.VK_L);
		c.setSelected(false);
		c.addItemListener(new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent i) {
				if(i.getStateChange()==ItemEvent.SELECTED){
					Communicator.s.doLog(true);
					System.out.println("Logging on");
				}
				else{
					Communicator.s.doLog(false);
					System.out.println("Logging off");
				}
			}	
		});*/
		
		
		this.add(c, BorderLayout.NORTH);
	}
	
	/* ***** Update screen ******* */
	
	
	/*public void updateAccel(float x, float y, float z){
		if(accel==null){
			accel = new float[]{0f,0f,0f};
		}
		
		accel[0] = x;
		accel[1] = y;
		accel[2] = z;
		
		updateLabels();
	}*/

	public void appendToPane(String s){
		debugStatus.setText(String.format("%s\n%s", debugStatus.getText(), s));
	
		updateLabels();
	}
	
	public void updateDelay(long lastSystemTime){
		commsDelay = lastTime - lastSystemTime;
		lastTime = lastSystemTime;

		updateLabels();
	}
	
	/* ***** REPAINT Screen ******* */

	public void updateLabels(){
		/*if(accelLabel!=null){
			accelLabel.setText(String.format("Accel: (%f,%f,%f)", accel[0],accel[1],accel[2]));
		}*/
		this.setBorder(BorderFactory.createTitledBorder(id.toUpperCase()));
		
		if(usersLabel!=null){
			usersLabel.setText(String.format("User count: %d", usersPanel.userCount()));
		}
		scroll.getVerticalScrollBar().setValue(scroll.getVerticalScrollBar().getMaximum());
		scroll.validate();
		//delayLabel.setText(String.format("Comms Delay: %.2f s", commsDelay/1000f));
		this.validate();
	}
}

